package com.huiyujinrong.qiangdanbao.activity.my;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单状态
 */

public class OrderStatusActivity extends AppCompatActivity implements OrderStatusContract.View{

    @BindView(R.id.toolbar_Title)
    TextView toolbar_title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout toolbar_back;

    @BindView(R.id.shenkeing)
    LinearLayout shenkeing;

    @BindView(R.id.item_2)
    RelativeLayout item2;  //审核状态  通过  拒绝
    @BindView(R.id.item_3)
    RelativeLayout item3;  //审核状态  放款成功


    @BindView(R.id.tvOrderStatus)
    TextView tvOrderStatus;  //咧国是 依照loan_status显示不同的内容跟到操作

    private Animation mHiddenAction, mShowAction;  //显示 隐藏  效果

    private String data1="", data2="", data3="", data4="";

    private int loan_status;  // 状态

    private String order_id;  //订单ID

    private int status;  //状态

    private ProgressDialog progressDialog;

    private OrderStatusPresenter presenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_status);
        ButterKnife.bind(this);

        setToolbar();

        setConfiguration();

        setStartAnimation();

        Intent intent = getIntent();
        loan_status=intent.getIntExtra("loan_status",0);
        order_id=intent.getStringExtra("order_id");

        switch (loan_status){
            case 2:
                tvOrderStatus.setText("审核中");
                showItem(1);
                break;
            case 3:
                tvOrderStatus.setText("审核通过");
                showItem(2);
                break;
            case 4:
                tvOrderStatus.setText("审核拒绝");
                break;
            case 5:
                tvOrderStatus.setText("放款成功");
        }

    }

    //切换 动效
    private void setStartAnimation(){
        mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -0.2f);
        mHiddenAction.setDuration(400);

        mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -0.2f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction.setDuration(400);
    }

    /**
     * 配置
     */
    private void setConfiguration(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);

        presenter = new OrderStatusPresenter(this);
        presenter.start();
    }

    /**
     * 状态  审核
     *
     * @param itemNum
     */
    private void showItem(int itemNum) {
        switch (itemNum) {
            case 1:
                /////////////状态码  3   通过///////////////
                status=3;
                // 通过 and 拒绝
                item2.setVisibility(View.VISIBLE);
                item3.setVisibility(View.GONE);
                break;
            case 2:
                /////////////状态码  5   放款成功///////////////
                status=5;
                //放款成功
                item2.setVisibility(View.GONE);
                item3.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void setToolbar() {
        //默认隐藏 多选  拒绝原因
        shenkeing.setVisibility(View.GONE);

        toolbar_title.setText("订单状态");
        toolbar_back.setVisibility(View.VISIBLE);
        toolbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    //切换 审核装状态
    @OnClick({R.id.radio_fused, R.id.radio_reFused})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radio_fused:
                /////////////状态码  3   审核通过///////////////
                status=3;
                //隐藏 拒绝原因
                shenkeing.startAnimation(mHiddenAction);
                shenkeing.setVisibility(View.GONE);
                break;
            case R.id.radio_reFused:

                status=4;
                //显示 拒绝原因
                shenkeing.startAnimation(mShowAction);
                shenkeing.setVisibility(View.VISIBLE);
                break;
        }
    }


    //拒绝原因 点击事件
    @BindView(R.id.check_box1)
    CheckBox checkBox1;
    @BindView(R.id.check_box2)
    CheckBox checkBox2;
    @BindView(R.id.check_box3)
    CheckBox checkBox3;
    @BindView(R.id.check_box4)
    CheckBox checkBox4;

    @OnClick({R.id.check_box1, R.id.check_box2, R.id.check_box3, R.id.check_box4})
    void checkBoxs(View view) {
        switch (view.getId()) {
            case R.id.check_box1:
                if (checkBox1.isChecked()) {
                    data1 = checkBox1.getText().toString();
                } else {
                    data1 = "";
                }
                break;
            case R.id.check_box2:
                if (checkBox2.isChecked()) {
                    data2 = checkBox2.getText().toString();
                } else {
                    data2 = "";
                }
                break;
            case R.id.check_box3:
                if (checkBox3.isChecked()) {
                    data3 = checkBox3.getText().toString();
                } else {
                    data3 = "";
                }
                break;
            case R.id.check_box4:
                if (checkBox4.isChecked()) {
                    data4 = checkBox4.getText().toString();
                } else {
                    data4 = "";
                }
                break;

        }
    }


    /**
     *提交
     */
    @OnClick(R.id.order_submit)
    void order_submit(){
       String data=data1+data2+data3+data4;
        presenter.submitOrderStatus(order_id,status,data);
    }

    @Override
    public void disPlay() {
        progressDialog.setMessage("提交中...");
        progressDialog.show();
    }

    @Override
    public void disMiss() {
        progressDialog.dismiss();
    }

    @Override
    public void showToast(String str) {
        Toast t = null;
        if(t==null){
            t=Toast.makeText(this,str,Toast.LENGTH_SHORT);
        }else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void submitOK() {
        finish();
    }

    @Override
    public void submitNO() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }
}
